/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.base.controller;

import com.xy.biz.base.client.RegionClient;
import com.xy.biz.base.domain.converter.RegionConverter;
import com.xy.biz.base.domain.dto.RegionDTO;
import com.xy.biz.base.domain.query.RegionQuery;
import com.xy.biz.base.domain.req.ListRegionRequest;
import com.xy.biz.base.service.RegionService;
import com.xy.lang.domain.Result;
import com.xy.lang.domain.treeing.Tree;
import com.xy.lang.domain.treeing.TreeItem;
import com.xy.lang.domain.treeing.TreeItemWrapper;
import com.xy.lang.domain.treeing.TreeNode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 *
 *
 * @author zsp
 * @date 2020-8-26
 */
@Slf4j
@RestController
@RequestMapping("/api/base/region")
public class RegionController implements RegionClient {

    @Autowired
    private RegionService regionService;

    @PostMapping(value = "/list")
    @Override
    public Result<List<RegionDTO>> listRegion(@RequestBody ListRegionRequest request) {
        return Result.success(regionService.listRegion(RegionConverter.toQuery(request)));
    }

    @PostMapping(value = "/list_by_id")
    @Override
    public Result<List<RegionDTO>> listRegionById(@RequestBody Collection<Long> collection) {
        return Result.success(regionService.listRegionById(collection));
    }

    @PostMapping(value = "/list_tree_item")
    public Result<List<TreeItem>> listRegionTreeItem() {
        List<RegionDTO> allRegionList = regionService.listRegion(RegionQuery.builder()
                .offset(0)
                .rows(3500)
                .build());
        if (CollectionUtils.isEmpty(allRegionList)) {
            return Result.success(Collections.emptyList());
        }
        return Result.success(Tree.buildTreeItem(allRegionList,
                e -> e.getSource().getRegionId(),
                e -> e.getSource().getParentRegionId(),
                e -> e == null || e == 0,
                e -> e.getRegionId().toString(),
                e -> e.getRegionName()));
    }

    @PostMapping(value = "/list_tree_item_text")
    public Result<List<TreeItem>> listRegionTreeItemText(@RequestBody RegionLevelRequest regionLevelRequest) {
        RegionQuery regionQuery = RegionQuery.builder()
                .offset(0)
                .rows(3500)
                .build();
        if (Objects.equals("1", regionLevelRequest.getLevel())) {
            regionQuery.setOneLevel("yes");
        } else if (Objects.equals("2", regionLevelRequest.getLevel())) {
            regionQuery.setTowLevel("yes");
        }
        List<RegionDTO> allRegionList = regionService.listRegion(regionQuery);
        if (CollectionUtils.isEmpty(allRegionList)) {
            return Result.success(Collections.emptyList());
        }
        return Result.success(Tree.buildTreeItem(allRegionList,
                e -> e.getSource().getRegionId(),
                e -> e.getSource().getParentRegionId(),
                e -> e == null || e == 0,
                e -> e.getRegionId() + "",
                e -> e.getRegionName()));
    }

    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    public static class RegionLevelRequest {
        private String level;
    }

}
